from django.db import models
from django.utils.html import format_html
from datetime import datetime

# Create your models here.
class Article(models.Model):
    id = models.IntegerField(primary_key=True)
    group_id = models.IntegerField()
    article_name = models.CharField('标题', max_length=200)
    url = models.CharField(max_length=200)
    logo = models.CharField('图片url', max_length=50)
    article_time = models.DateField('发布时间')
    author = models.CharField('作者', max_length=20)
    source = models.CharField('文章来源', max_length=20)
    status = models.IntegerField('状态', default=0)
    creat_time = models.DateTimeField(auto_now=True)

    def colored_status(self):
        if self.status == 1:
            status_color = "已推送"
            color_code = "green"
        else:
            status_color = "未推送"
            color_code = "red"
        return format_html(
            '<span style="color: {};">{}</span>',
            color_code,
            status_color,
        )

    colored_status.short_description = u"状态"

    def link_url(self):
        return format_html(
            '<a href="{}" target="_blank">{}</a>',
            self.url,
            self.url,
        )
    link_url.short_description = u"文章url"



    class Meta:
        db_table = 'bq_articles'
        verbose_name_plural = '文章'